package com.xyz.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xyz.config.SystemData;
import com.xyz.dao.mapper.UpkeepDaoMapper;
import com.xyz.dto.*;
import com.xyz.dto.enums.EnumAcceptBusinessType;
import com.xyz.dto.enums.EnumBusinessType;
import com.xyz.dto.enums.EnumRepState;
import com.xyz.entity.mapper.*;
import com.xyz.entity.pojo.*;
import com.xyz.service.CommonService;
import com.xyz.service.UpkeepService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class UpkeepServiceImpl implements UpkeepService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private UpkeepWorkOrderMapper upkeepWorkOrderMapper;

    @Autowired
    private UpkeepMaintenanceMapper upkeepMaintenanceMapper;

    @Autowired
    private UpkeepPartMapper upkeepPartMapper;

    @Autowired
    private UpkeepTaskMapper upkeepTaskMapper;

    @Autowired
    private SystemParameterMapper systemParameterMapper;

    @Autowired
    private UpkeepWorkOrderFlowMapper upkeepWorkOrderFlowMapper;

    @Autowired
    private UpkeepMaintenanceSuspendMapper upkeepMaintenanceSuspendMapper;

    @Autowired
    private BusinessFileMapper businessFileMapper;

    @Autowired
    private BusinessAcceptMapper businessAcceptMapper;

    @Autowired
    private UpkeepDaoMapper upkeepDaoMapper;

    /*
     * 新增保养
     * */
    @Override
    public DataResult insertUpkeep(LoginInfo loginInfo, UpkeepWorkOrderDto dto) throws Exception {
        UpkeepWorkOrder upkeepWorkOrder = new UpkeepWorkOrder();
        upkeepWorkOrder.setPlanId(0L);
        long emId = Long.parseLong(dto.getEmId());
        if (emId == 0) {
            if (StrUtil.isBlank(dto.getEmLocation())) {
                return DataResult.build9200("选择其他设备时,设备位置需要填写！");
            }
            upkeepWorkOrder.setTreeId(0L);
            upkeepWorkOrder.setTreeName("");
        } else {
            EquipmentTree EquipmentTree2 = commonService.getTree2ByEmId(emId);
            if (EquipmentTree2 != null) {
                upkeepWorkOrder.setTreeId(EquipmentTree2.getId());
                upkeepWorkOrder.setTreeName(EquipmentTree2.getName());
            }
        }
        upkeepWorkOrder.setEmId(emId);
        upkeepWorkOrder.setEmName(dto.getEmName());
        upkeepWorkOrder.setWorkOrderCode(commonService.getBxWorkOrderCode());
        if (StrUtil.isNotBlank(dto.getEmCode())) {
            upkeepWorkOrder.setEmCode(dto.getEmCode());
        } else {
            upkeepWorkOrder.setEmCode("");
        }

        if (StrUtil.isNotBlank(dto.getEmSpecification())) {
            upkeepWorkOrder.setEmSpecification(dto.getEmSpecification());
        } else {
            upkeepWorkOrder.setEmSpecification("");
        }

        if (StrUtil.isNotBlank(dto.getEmLocation())) {
            upkeepWorkOrder.setEmLocation(dto.getEmLocation());
        } else {
            upkeepWorkOrder.setEmLocation("");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        upkeepWorkOrder.setUpkeepState(EnumRepState.PENDING_ANNOUNCE.getRepState());
        upkeepWorkOrder.setEmployeeId(loginInfo.getLoginEmployeeId());
        upkeepWorkOrder.setUpkeepStartTime(sdf.parse(dto.getUpkeepStartTime()));
        upkeepWorkOrder.setCreateTime(new Date());
        upkeepWorkOrderMapper.insertSelective(upkeepWorkOrder);
        //保存任务项
        List<UpkeepTask> upkeepTaskList = new ArrayList<>();
        JSONArray taskJSONArray = JSONUtil.parseArray(dto.getTaskList());
        for (Object obj : taskJSONArray) {
            JSONObject jsonObject = JSONUtil.parseObj(obj);
            Long taskId = jsonObject.getLong("taskId");
            String taskName = jsonObject.getStr("taskName");
            String taskDesc = jsonObject.getStr("taskDesc");
            String remark = jsonObject.getStr("remark");
            String precautions = jsonObject.getStr("precautions");
            if (taskId != null) {
                UpkeepTask upkeepTask = new UpkeepTask();
                upkeepTask.setUpkeepId(upkeepWorkOrder.getUpkeepId());
                upkeepTask.setTaskId(taskId);
                upkeepTask.setTaskName(taskName);
                upkeepTask.setTaskDesc(taskDesc);
                upkeepTask.setRemark(remark);
                upkeepTask.setPrecautions(precautions);
                upkeepTask.setIsComplete(1);
                upkeepTask.setCreateTime(new Date());
                upkeepTaskList.add(upkeepTask);
            }
        }
        if (CollUtil.isNotEmpty(upkeepTaskList)) {
            upkeepDaoMapper.insertUpkeepTaskList(upkeepTaskList);
        }
        //保存零件
        if (StrUtil.isNotBlank(dto.getPartList())) {
            List<UpkeepPart> upkeepPartList = new ArrayList<>();
            JSONArray jSONArray = JSONUtil.parseArray(dto.getPartList());
            for (Object obj : jSONArray) {
                JSONObject jsonObject = JSONUtil.parseObj(obj);
                Long partId = jsonObject.getLong("partId");
                Integer partNum = jsonObject.getInt("partNum");
                if (partId != null && partNum != null) {
                    UpkeepPart upkeepPart = new UpkeepPart();
                    upkeepPart.setUpkeepId(upkeepWorkOrder.getUpkeepId());
                    upkeepPart.setPartId(partId);
                    upkeepPart.setPartNum(partNum);
                    upkeepPart.setIsComplete(1);
                    upkeepPart.setCreateTime(new Date());
                    upkeepPartList.add(upkeepPart);
                }
            }
            if (CollUtil.isNotEmpty(upkeepPartList)) {
                upkeepDaoMapper.insertUpkeepPartList(upkeepPartList);
            }
        }
        insertFlow(upkeepWorkOrder.getUpkeepId(), loginInfo.getLoginEmployeeId(), 1, upkeepWorkOrder.getUpkeepId(), EnumRepState.PENDING_ANNOUNCE.getRepState(), "创建维保工单");

        List<Long> userIdList = commonService.selectManagerUserIdList(3);
        for (Long userId : userIdList) {
            //待办
            commonService.insertMessageBacklog(
                    userId,
                    "新增维保单待审核，请处理",
                    dto.getEmName() + "新增一个维保单，地点在【" + dto.getEmLocation() + "】，请审核维保申请信息。",
                    3, 1, upkeepWorkOrder.getUpkeepId(), upkeepWorkOrder.getWorkOrderCode()
            );
        }
        Map<String, Object> restMap = new HashMap<>();
        restMap.put("upkeepId", upkeepWorkOrder.getUpkeepId());
        return DataResult.build9100(restMap);
    }

    /*
     * 编辑保养
     * */
    @Override
    public DataResult updateUpkeep(LoginInfo loginInfo, UpkeepWorkOrderDto dto) throws Exception {
        if (StrUtil.isBlank(dto.getUpkeepId())) {
            return DataResult.build9200("upkeepId数据缺失");
        }

        long UpkeepId = Long.parseLong(dto.getUpkeepId());
        UpkeepWorkOrder upkeepWorkOrder = upkeepWorkOrderMapper.selectByPrimaryKey(UpkeepId);

        if (upkeepWorkOrder == null) {
            return DataResult.build9250("获取工单信息失败！");
        }

        if (!upkeepWorkOrder.getUpkeepState().equals(EnumRepState.PENDING_ANNOUNCE.getRepState())) {
            return DataResult.build9250("待审核的工单才能编辑修改!");
        }

        if (upkeepWorkOrder.getEmployeeId() != 0) {
            if (!upkeepWorkOrder.getEmployeeId().equals(loginInfo.getLoginEmployeeId())) {
                return DataResult.build9250("当前人员不是创建人员，不能修改!");
            }
        }
        long emId = Long.parseLong(dto.getEmId());
        if (emId == 0) {
            if (StrUtil.isBlank(dto.getEmLocation())) {
                return DataResult.build9200("选择其他设备时,设备位置需要填写！");
            }
        }
        upkeepWorkOrder.setEmId(emId);
        upkeepWorkOrder.setEmName(dto.getEmName());
        if (StrUtil.isNotBlank(dto.getEmCode())) {
            upkeepWorkOrder.setEmCode(dto.getEmCode());
        } else {
            upkeepWorkOrder.setEmCode("");
        }

        if (StrUtil.isNotBlank(dto.getEmSpecification())) {
            upkeepWorkOrder.setEmSpecification(dto.getEmSpecification());
        } else {
            upkeepWorkOrder.setEmSpecification("");
        }

        if (StrUtil.isNotBlank(dto.getEmLocation())) {
            upkeepWorkOrder.setEmLocation(dto.getEmLocation());
        } else {
            upkeepWorkOrder.setEmLocation("");
        }
        upkeepWorkOrder.setEmployeeId(loginInfo.getLoginEmployeeId());
        upkeepWorkOrderMapper.updateByPrimaryKeySelective(upkeepWorkOrder);

        UpkeepTaskExample taskExample = new UpkeepTaskExample();
        taskExample.createCriteria().andUpkeepIdEqualTo(upkeepWorkOrder.getUpkeepId());
        upkeepTaskMapper.deleteByExample(taskExample);

        UpkeepPartExample upkeepPartExample = new UpkeepPartExample();
        upkeepPartExample.createCriteria().andUpkeepIdEqualTo(upkeepWorkOrder.getUpkeepId());
        upkeepPartMapper.deleteByExample(upkeepPartExample);

        //保存任务项
        List<UpkeepTask> upkeepTaskList = new ArrayList<>();
        JSONArray taskJSONArray = JSONUtil.parseArray(dto.getTaskList());
        for (Object obj : taskJSONArray) {
            JSONObject jsonObject = JSONUtil.parseObj(obj);
            Long taskId = jsonObject.getLong("taskId");
            String taskName = jsonObject.getStr("taskName");
            String taskDesc = jsonObject.getStr("taskDesc");
            String remark = jsonObject.getStr("remark");
            String precautions = jsonObject.getStr("precautions");
            if (taskId != null) {
                UpkeepTask upkeepTask = new UpkeepTask();
                upkeepTask.setUpkeepId(upkeepWorkOrder.getUpkeepId());
                upkeepTask.setTaskId(taskId);
                upkeepTask.setTaskName(taskName);
                upkeepTask.setTaskDesc(taskDesc);
                upkeepTask.setRemark(remark);
                upkeepTask.setPrecautions(precautions);
                upkeepTask.setIsComplete(1);
                upkeepTask.setCreateTime(new Date());
                upkeepTaskList.add(upkeepTask);
            }
        }
        if (CollUtil.isNotEmpty(upkeepTaskList)) {
            upkeepDaoMapper.insertUpkeepTaskList(upkeepTaskList);
        }
        //保存零件
        if (StrUtil.isNotBlank(dto.getPartList())) {
            List<UpkeepPart> upkeepPartList = new ArrayList<>();
            JSONArray jSONArray = JSONUtil.parseArray(dto.getPartList());
            for (Object obj : jSONArray) {
                JSONObject jsonObject = JSONUtil.parseObj(obj);
                Long partId = jsonObject.getLong("partId");
                Integer partNum = jsonObject.getInt("partNum");
                if (partId != null && partNum != null) {
                    UpkeepPart upkeepPart = new UpkeepPart();
                    upkeepPart.setUpkeepId(upkeepWorkOrder.getUpkeepId());
                    upkeepPart.setPartId(partId);
                    upkeepPart.setPartNum(partNum);
                    upkeepPart.setIsComplete(1);
                    upkeepPart.setCreateTime(new Date());
                    upkeepPartList.add(upkeepPart);
                }
            }
            if (CollUtil.isNotEmpty(upkeepPartList)) {
                upkeepDaoMapper.insertUpkeepPartList(upkeepPartList);
            }
        }
        insertFlow(upkeepWorkOrder.getUpkeepId(), loginInfo.getLoginEmployeeId(), 1, upkeepWorkOrder.getUpkeepId(), EnumRepState.PENDING_ANNOUNCE.getRepState(), "编辑维保工单");
        return DataResult.build9100();
    }

    /*
     * 领取工单
     * receiveType = 1 自己领取
     * receiveType = 2 领导派单
     * */
    @Override
    public DataResult receiveUpkeep(LoginInfo loginInfo, String upkeepId, String receiveType, String employeeId) throws Exception {
        long UpkeepIdLong = Long.parseLong(upkeepId);
        UpkeepWorkOrder getUpkeepWorkOrder = upkeepWorkOrderMapper.selectByPrimaryKey(UpkeepIdLong);
        if (getUpkeepWorkOrder == null) {
            return DataResult.build9250("获取工单信息失败！");
        }
        Integer upkeepStateInt = getUpkeepWorkOrder.getUpkeepState();
        if (!upkeepStateInt.equals(EnumRepState.PENDING_RECEIVE.getRepState())
                && !upkeepStateInt.equals(EnumRepState.PENDING_REFUSE.getRepState())
                && !upkeepStateInt.equals(EnumRepState.PENDING_TRANSFER.getRepState())) {
            return DataResult.build9250("只有待领取状态的工单才能领取工单！！");
        }
        //修改维修单状态
        UpkeepMaintenance upkeepMaintenance = new UpkeepMaintenance();
        upkeepMaintenance.setUpkeepId(UpkeepIdLong);
        upkeepMaintenance.setDisTime(new Date());
        upkeepMaintenance.setMtcState(EnumRepState.ALLOCATED.getRepState());
        upkeepMaintenance.setCreateTime(new Date());
        //修改维修工单状态:1,自己领取:2,领导派单
        String flowDesc = "";
        if ("1".equals(receiveType)) {
            if (upkeepStateInt.equals(EnumRepState.PENDING_REFUSE.getRepState())
                    || upkeepStateInt.equals(EnumRepState.PENDING_TRANSFER.getRepState())) {
                return DataResult.build9250("拒单和转交的工单只能领导分配，不能自己领取！！");
            }
            //分配类型:2自己抢单；3管理人员分配
            upkeepMaintenance.setDisType(2);
            //分配人
            upkeepMaintenance.setDisEmployeeId(loginInfo.getLoginEmployeeId());
            //维修人
            upkeepMaintenance.setEmployeeId(loginInfo.getLoginEmployeeId());
            flowDesc = "领取工单";
        } else if ("2".equals(receiveType)) {
            if (StrUtil.isNotBlank(employeeId)) {
                upkeepMaintenance.setDisType(3);
                upkeepMaintenance.setDisEmployeeId(loginInfo.getLoginEmployeeId());
                upkeepMaintenance.setEmployeeId(Long.parseLong(employeeId));
                flowDesc = "指派工单";
            } else {
                return DataResult.build9250("派单请选择维修人员!!!");
            }
        }
        //修改工单状态
        UpkeepWorkOrder upkeepWorkOrder = new UpkeepWorkOrder();
        upkeepWorkOrder.setUpkeepId(UpkeepIdLong);
        upkeepWorkOrder.setMaintenEmployeeId(upkeepMaintenance.getEmployeeId());
        upkeepWorkOrder.setUpkeepState(EnumRepState.ALLOCATED.getRepState());
        upkeepWorkOrderMapper.updateByPrimaryKeySelective(upkeepWorkOrder);
        upkeepMaintenanceMapper.insertSelective(upkeepMaintenance);
        //记录流水
        insertDisFlow(UpkeepIdLong, upkeepMaintenance.getEmployeeId(), 3, upkeepMaintenance.getId(), EnumRepState.ALLOCATED.getRepState(), flowDesc, loginInfo.getLoginEmployeeId());
        //待办
        commonService.insertMessageBacklog(
                upkeepMaintenance.getEmployeeId(),
                "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】维保单已分配到您，请处理",
                "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】已分配到您，维修设备：【" + getUpkeepWorkOrder.getEmCode() + "】-【" + getUpkeepWorkOrder.getEmName() + "】，地点在【" + getUpkeepWorkOrder.getEmLocation() + "】，工单在【30分钟】后自动进入处理中，请尽快到场处理。",
                3, 2, getUpkeepWorkOrder.getUpkeepId(), getUpkeepWorkOrder.getWorkOrderCode()
        );
        return DataResult.build9100();
    }

    /*
     * 拒单
     * */
    @Override
    public DataResult refuseUpkeep(LoginInfo loginInfo, String id) throws Exception {
        UpkeepMaintenance upkeepMaintenance = upkeepMaintenanceMapper.selectByPrimaryKey(Long.parseLong(id));
        if (upkeepMaintenance == null) {
            return DataResult.build9250("获取维保人员信息失败！");
        }
        if (!upkeepMaintenance.getEmployeeId().equals(loginInfo.getLoginEmployeeId())) {
            String errName = commonService.getEmployeeNameById(upkeepMaintenance.getEmployeeId());
            return DataResult.build9250("人员" + errName + "才能操作");
        }

        if (!upkeepMaintenance.getMtcState().equals(EnumRepState.ALLOCATED.getRepState())) {
            return DataResult.build9250("已分配的工单才能拒单!");
        }

        if (upkeepMaintenance.getDisType() == 2) {
            return DataResult.build9250("自己领取的工单不能拒单!");
        }

        UpkeepMaintenance updateUpkeepMaintenance = new UpkeepMaintenance();
        updateUpkeepMaintenance.setId(upkeepMaintenance.getId());
        updateUpkeepMaintenance.setMtcState(EnumRepState.INVALID.getRepState());
        //processType;3,拒单
        updateUpkeepMaintenance.setProcessType(3);
        upkeepMaintenanceMapper.updateByPrimaryKeySelective(updateUpkeepMaintenance);
        UpkeepWorkOrder updateUpkeepWorkOrder = new UpkeepWorkOrder();
        updateUpkeepWorkOrder.setUpkeepId(upkeepMaintenance.getUpkeepId());
        updateUpkeepWorkOrder.setUpkeepState(EnumRepState.PENDING_REFUSE.getRepState());
        upkeepWorkOrderMapper.updateByPrimaryKeySelective(updateUpkeepWorkOrder);
        //记录流水
        insertFlow(upkeepMaintenance.getUpkeepId(), upkeepMaintenance.getEmployeeId(), 3, upkeepMaintenance.getId(), EnumRepState.PENDING_REFUSE.getRepState(), "拒单");

        UpkeepWorkOrder getUpkeepWorkOrder = upkeepWorkOrderMapper.selectByPrimaryKey(upkeepMaintenance.getUpkeepId());

        List<Long> userIdList = commonService.selectManagerUserIdList(3);
        for (Long userId : userIdList) {
            //待办
            commonService.insertMessageBacklog(
                    userId,
                    "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】已退回，请重新分配",
                    "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】已退回，退回原因：【拒单原因】，请重新分配工作。",
                    3, 3, getUpkeepWorkOrder.getUpkeepId(), getUpkeepWorkOrder.getWorkOrderCode()
            );
        }

        return DataResult.build9100();
    }

    /*
     * 手动点击维保中
     * */
    @Override
    public DataResult undeUpkeep(LoginInfo loginInfo, String id) throws Exception {
        UpkeepMaintenance upkeepMaintenance = upkeepMaintenanceMapper.selectByPrimaryKey(Long.parseLong(id));
        if (upkeepMaintenance == null) {
            return DataResult.build9250("获取维保人员信息失败！");
        }

        if (!upkeepMaintenance.getMtcState().equals(EnumRepState.ALLOCATED.getRepState())) {
            return DataResult.build9250("已分配的工单才能开始维保中!");
        }

        if (!upkeepMaintenance.getEmployeeId().equals(loginInfo.getLoginEmployeeId())) {
            String errName = commonService.getEmployeeNameById(upkeepMaintenance.getEmployeeId());
            return DataResult.build9250("人员" + errName + "才能操作");
        }

        UpkeepMaintenance updateUpkeepMaintenance = new UpkeepMaintenance();
        updateUpkeepMaintenance.setId(upkeepMaintenance.getId());
        updateUpkeepMaintenance.setMtcState(EnumRepState.UNDE_REPAIR.getRepState());
        //processType;1,系统计时自动，2，手动点击
        updateUpkeepMaintenance.setProcessType(2);
        updateUpkeepMaintenance.setMtcStartTime(new Date());
        upkeepMaintenanceMapper.updateByPrimaryKeySelective(updateUpkeepMaintenance);
        UpkeepWorkOrder updateUpkeepWorkOrder = new UpkeepWorkOrder();
        updateUpkeepWorkOrder.setUpkeepId(upkeepMaintenance.getUpkeepId());
        updateUpkeepWorkOrder.setUpkeepState(EnumRepState.UNDE_REPAIR.getRepState());
        upkeepWorkOrderMapper.updateByPrimaryKeySelective(updateUpkeepWorkOrder);
        //记录流水
        insertFlow(upkeepMaintenance.getUpkeepId(), upkeepMaintenance.getEmployeeId(), 3, upkeepMaintenance.getId(), EnumRepState.UNDE_REPAIR.getRepState(), "维保中");
        return DataResult.build9100();
    }

    /*
     * 暂停和开始维保
     * */
    @Override
    public DataResult suspendUpkeep(LoginInfo loginInfo, String id, String type) throws Exception {
        UpkeepMaintenance upkeepMaintenance = upkeepMaintenanceMapper.selectByPrimaryKey(Long.parseLong(id));
        if (upkeepMaintenance == null) {
            return DataResult.build9250("获取维保人员信息失败！");
        }
        if (!upkeepMaintenance.getEmployeeId().equals(loginInfo.getLoginEmployeeId())) {
            String errName = commonService.getEmployeeNameById(upkeepMaintenance.getEmployeeId());
            return DataResult.build9250("人员" + errName + "才能操作");
        }
        int mtcState = upkeepMaintenance.getMtcState();
        //修改维修表状态
        UpkeepMaintenance updateUpkeepMaintenance = new UpkeepMaintenance();
        updateUpkeepMaintenance.setId(upkeepMaintenance.getId());
        //工单表状态同步
        UpkeepWorkOrder updateUpkeepWorkOrder = new UpkeepWorkOrder();
        updateUpkeepWorkOrder.setUpkeepId(upkeepMaintenance.getUpkeepId());
        int flowState = 0;
        String flowDesc = "";
        if ("2".equals(type)) {
            if (mtcState != EnumRepState.SUSPEND_REPAIR.getRepState()) {
                return DataResult.build9250("暂停中的工单才能恢复维保中!");
            }
            updateUpkeepWorkOrder.setUpkeepState(EnumRepState.UNDE_REPAIR.getRepState());
            updateUpkeepMaintenance.setMtcState(EnumRepState.UNDE_REPAIR.getRepState());
            flowState = EnumRepState.UNDE_REPAIR.getRepState();
            flowDesc = "开始维保";
            //暂停时间计算
            UpkeepMaintenanceSuspendExample msExample = new UpkeepMaintenanceSuspendExample();
            UpkeepMaintenanceSuspendExample.Criteria msCriteria = msExample.createCriteria();
            msCriteria.andMaintenanceIdEqualTo(upkeepMaintenance.getId());
            msCriteria.andStateEqualTo(1);
            msExample.setOrderByClause("start_time desc limit 1");
            List<UpkeepMaintenanceSuspend> upkeepMaintenanceSuspends = upkeepMaintenanceSuspendMapper.selectByExample(msExample);
            if (CollUtil.isNotEmpty(upkeepMaintenanceSuspends)) {
                UpkeepMaintenanceSuspend getUpkeepMaintenanceSuspend = upkeepMaintenanceSuspends.get(0);
                UpkeepMaintenanceSuspend upkeepMaintenanceSuspend = new UpkeepMaintenanceSuspend();
                upkeepMaintenanceSuspend.setId(getUpkeepMaintenanceSuspend.getId());
                upkeepMaintenanceSuspend.setState(2);
                upkeepMaintenanceSuspend.setEndTime(new Date());
                long between = DateUtil.between(getUpkeepMaintenanceSuspend.getStartTime(), upkeepMaintenanceSuspend.getEndTime(), DateUnit.SECOND);
                upkeepMaintenanceSuspend.setSuspendSecond(between);
                upkeepMaintenanceSuspendMapper.updateByPrimaryKeySelective(upkeepMaintenanceSuspend);
            }
        } else {
            if (mtcState != EnumRepState.UNDE_REPAIR.getRepState()) {
                return DataResult.build9250("维保中的工单才能暂停!");
            }
            flowState = EnumRepState.SUSPEND_REPAIR.getRepState();
            flowDesc = "暂停维保";
            UpkeepMaintenanceSuspend upkeepMaintenanceSuspend = new UpkeepMaintenanceSuspend();
            upkeepMaintenanceSuspend.setMaintenanceId(upkeepMaintenance.getId());
            upkeepMaintenanceSuspend.setStartTime(new Date());
            upkeepMaintenanceSuspend.setState(1);
            upkeepMaintenanceSuspendMapper.insertSelective(upkeepMaintenanceSuspend);
            updateUpkeepWorkOrder.setUpkeepState(EnumRepState.SUSPEND_REPAIR.getRepState());
            updateUpkeepMaintenance.setMtcState(EnumRepState.SUSPEND_REPAIR.getRepState());
        }
        upkeepMaintenanceMapper.updateByPrimaryKeySelective(updateUpkeepMaintenance);
        upkeepWorkOrderMapper.updateByPrimaryKeySelective(updateUpkeepWorkOrder);
        //记录流水
        insertFlow(upkeepMaintenance.getUpkeepId(), upkeepMaintenance.getEmployeeId(), 3, upkeepMaintenance.getId(), flowState, flowDesc);
        return DataResult.build9100();
    }

    /*
     * 工单转交
     * */
    @Override
    public DataResult transferUpkeep(LoginInfo loginInfo, String id) throws Exception {
        UpkeepMaintenance upkeepMaintenance = upkeepMaintenanceMapper.selectByPrimaryKey(Long.parseLong(id));
        if (upkeepMaintenance == null) {
            return DataResult.build9250("获取维修人员信息失败！");
        }

        if (!upkeepMaintenance.getEmployeeId().equals(loginInfo.getLoginEmployeeId())) {
            String errName = commonService.getEmployeeNameById(upkeepMaintenance.getEmployeeId());
            return DataResult.build9250("人员" + errName + "才能操作");
        }

        if (!EnumRepState.SUSPEND_REPAIR.getRepState().equals(upkeepMaintenance.getMtcState())) {
            return DataResult.build9250("暂停的工单才能转交!");
        }

        UpkeepMaintenance updateUpkeepMaintenance = new UpkeepMaintenance();
        updateUpkeepMaintenance.setId(upkeepMaintenance.getId());
        updateUpkeepMaintenance.setMtcState(EnumRepState.INVALID.getRepState());
        //processType;4,转交
        updateUpkeepMaintenance.setProcessType(4);
        upkeepMaintenanceMapper.updateByPrimaryKeySelective(updateUpkeepMaintenance);
        UpkeepWorkOrder updateUpkeepWorkOrder = new UpkeepWorkOrder();
        updateUpkeepWorkOrder.setUpkeepId(upkeepMaintenance.getUpkeepId());
        updateUpkeepWorkOrder.setUpkeepState(EnumRepState.PENDING_TRANSFER.getRepState());
        upkeepWorkOrderMapper.updateByPrimaryKeySelective(updateUpkeepWorkOrder);
        //记录流水
        insertFlow(upkeepMaintenance.getUpkeepId(), upkeepMaintenance.getEmployeeId(), 3, upkeepMaintenance.getId(), EnumRepState.PENDING_TRANSFER.getRepState(), "工单转交");
        return DataResult.build9100();
    }

    /*
     * 维保保存
     * */
    @Override
    public DataResult insertSuspendUpkeep(LoginInfo loginInfo, UpkeepMaintenanceInsDto upkeepMaintenanceInsDto) throws Exception {
        UpkeepMaintenance upkeepMaintenance = upkeepMaintenanceMapper.selectByPrimaryKey(Long.parseLong(upkeepMaintenanceInsDto.getId()));
        if (upkeepMaintenance == null) {
            return DataResult.build9250("获取维修人员信息失败！");
        }

        if (!upkeepMaintenance.getEmployeeId().equals(loginInfo.getLoginEmployeeId())) {
            String errName = commonService.getEmployeeNameById(upkeepMaintenance.getEmployeeId());
            return DataResult.build9250("人员" + errName + "才能操作");
        }
        if (!EnumRepState.UNDE_REPAIR.getRepState().equals(upkeepMaintenance.getMtcState())) {
            return DataResult.build9250("维保中的工单才能操作!");
        }
        UpkeepMaintenance updateUpkeepMaintenance = new UpkeepMaintenance();
        updateUpkeepMaintenance.setId(upkeepMaintenance.getId());
        if (StrUtil.isBlank(upkeepMaintenanceInsDto.getRemark())) {
            updateUpkeepMaintenance.setRemark("");
        } else {
            updateUpkeepMaintenance.setRemark(upkeepMaintenanceInsDto.getRemark());
        }
        upkeepMaintenanceMapper.updateByPrimaryKeySelective(updateUpkeepMaintenance);
        //保存任务项
        JSONArray taskJSONArray = JSONUtil.parseArray(upkeepMaintenanceInsDto.getTaskList());
        for (Object obj : taskJSONArray) {
            JSONObject jsonObject = JSONUtil.parseObj(obj);
            Long id = jsonObject.getLong("id");
            Integer isComplete = jsonObject.getInt("isComplete");
            if (id != null && isComplete != null) {
                UpkeepTask upkeepTask = new UpkeepTask();
                upkeepTask.setId(id);
                upkeepTask.setIsComplete(isComplete);
                upkeepTaskMapper.updateByPrimaryKeySelective(upkeepTask);
            }
        }
        //图片保存
        BusinessFileExample fileExample = new BusinessFileExample();
        BusinessFileExample.Criteria fileCriteria = fileExample.createCriteria();
        fileCriteria.andBusTablesIdEqualTo(upkeepMaintenance.getId());
        fileCriteria.andBusTypeEqualTo(EnumBusinessType.UPKEEPMAINTENANCE.getBusType());
        businessFileMapper.deleteByExample(fileExample);
        insertMaintenanceFile(upkeepMaintenance.getId(), loginInfo.getLoginEmployeeId(), upkeepMaintenanceInsDto);
        return DataResult.build9100();
    }

    /*
     * 点击维保完成
     * */
    @Override
    public DataResult completeUpkeep(LoginInfo loginInfo, UpkeepMaintenanceInsDto upkeepMaintenanceInsDto) throws Exception {
        UpkeepMaintenance upkeepMaintenance = upkeepMaintenanceMapper.selectByPrimaryKey(Long.parseLong(upkeepMaintenanceInsDto.getId()));
        if (upkeepMaintenance == null) {
            return DataResult.build9250("获取维修人员信息失败！");
        }

        if (!upkeepMaintenance.getEmployeeId().equals(loginInfo.getLoginEmployeeId())) {
            String errName = commonService.getEmployeeNameById(upkeepMaintenance.getEmployeeId());
            return DataResult.build9250("人员" + errName + "才能操作");
        }
        if (!EnumRepState.UNDE_REPAIR.getRepState().equals(upkeepMaintenance.getMtcState())) {
            return DataResult.build9250("维保中的工单才能操作!");
        }
        //编辑数据
        UpkeepMaintenance updateUpkeepMaintenance = new UpkeepMaintenance();
        updateUpkeepMaintenance.setId(upkeepMaintenance.getId());
        updateUpkeepMaintenance.setMtcState(EnumRepState.ACCEPTED_NO.getRepState());
        updateUpkeepMaintenance.setMtcEndTime(new Date());
        if (StrUtil.isBlank(upkeepMaintenanceInsDto.getRemark())) {
            updateUpkeepMaintenance.setRemark("");
        } else {
            updateUpkeepMaintenance.setRemark(upkeepMaintenanceInsDto.getRemark());
        }
        //计算维修时间
        long between = DateUtil.between(upkeepMaintenance.getMtcStartTime(), updateUpkeepMaintenance.getMtcEndTime(), DateUnit.SECOND);
        UpkeepMaintenanceSuspendExample supendExample = new UpkeepMaintenanceSuspendExample();
        UpkeepMaintenanceSuspendExample.Criteria supendCriteria = supendExample.createCriteria();
        supendCriteria.andMaintenanceIdEqualTo(upkeepMaintenance.getId());
        supendCriteria.andStateEqualTo(2);
        List<UpkeepMaintenanceSuspend> upkeepMaintenanceSuspends = upkeepMaintenanceSuspendMapper.selectByExample(supendExample);
        long suspendSecond = 0;
        for (UpkeepMaintenanceSuspend suspendInfo : upkeepMaintenanceSuspends) {
            if (suspendInfo.getSuspendSecond() != 0) {
                suspendSecond += suspendInfo.getSuspendSecond();
            }
        }

        long countMtcSecond = between - suspendSecond;
        if (countMtcSecond < 0) {
            countMtcSecond = 0;
        }
        updateUpkeepMaintenance.setMtcSecond(countMtcSecond);
        updateUpkeepMaintenance.setSuspendSecond(suspendSecond);
        upkeepMaintenanceMapper.updateByPrimaryKeySelective(updateUpkeepMaintenance);
        //保存任务项
        JSONArray taskJSONArray = JSONUtil.parseArray(upkeepMaintenanceInsDto.getTaskList());
        for (Object obj : taskJSONArray) {
            JSONObject jsonObject = JSONUtil.parseObj(obj);
            Long id = jsonObject.getLong("id");
            Integer isComplete = jsonObject.getInt("isComplete");
            if (id != null && isComplete != null) {
                UpkeepTask upkeepTask = new UpkeepTask();
                upkeepTask.setId(id);
                upkeepTask.setIsComplete(isComplete);
                upkeepTaskMapper.updateByPrimaryKeySelective(upkeepTask);
            }
        }
        //图片保存
        BusinessFileExample fileExample = new BusinessFileExample();
        BusinessFileExample.Criteria fileCriteria = fileExample.createCriteria();
        fileCriteria.andBusTablesIdEqualTo(upkeepMaintenance.getId());
        fileCriteria.andBusTypeEqualTo(EnumBusinessType.UPKEEPMAINTENANCE.getBusType());
        businessFileMapper.deleteByExample(fileExample);
        insertMaintenanceFile(upkeepMaintenance.getId(), loginInfo.getLoginEmployeeId(), upkeepMaintenanceInsDto);

        UpkeepWorkOrder updateUpkeepWorkOrder = new UpkeepWorkOrder();
        updateUpkeepWorkOrder.setUpkeepId(upkeepMaintenance.getUpkeepId());
        updateUpkeepWorkOrder.setUpkeepState(EnumRepState.ACCEPTED_NO.getRepState());
        updateUpkeepWorkOrder.setUpkeepEndTime(new Date());
        upkeepWorkOrderMapper.updateByPrimaryKeySelective(updateUpkeepWorkOrder);
        //记录流水
        insertFlow(upkeepMaintenance.getUpkeepId(), upkeepMaintenance.getEmployeeId(), 3, upkeepMaintenance.getId(), EnumRepState.ACCEPTED_NO.getRepState(), "维保提交验收");

        UpkeepWorkOrder getUpkeepWorkOrder = upkeepWorkOrderMapper.selectByPrimaryKey(upkeepMaintenance.getUpkeepId());

        List<Long> userIdList = commonService.selectManagerUserIdList(3);
        for (Long userId : userIdList) {
            //待办
            commonService.insertMessageBacklog(
                    userId,
                    "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】已维保完工，请指定验收人员",
                    "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】已维保完工，请指定验收人员进行现场验收。维修设备：【" + getUpkeepWorkOrder.getEmCode() + "】-【" + getUpkeepWorkOrder.getEmName() + "】，地点在【" + getUpkeepWorkOrder.getEmLocation() + "】。",
                    3, 4, getUpkeepWorkOrder.getUpkeepId(), getUpkeepWorkOrder.getWorkOrderCode()
            );
        }

        return DataResult.build9100();
    }

    /*
     * 验收指派人员
     * */
    @Override
    public DataResult accepAssignUpkeep(LoginInfo loginInfo, String id, String employeeId) throws Exception {
        UpkeepMaintenance upkeepMaintenance = upkeepMaintenanceMapper.selectByPrimaryKey(Long.parseLong(id));
        if (upkeepMaintenance == null) {
            return DataResult.build9250("获取维修人员信息失败！");
        }
        if (!EnumRepState.ACCEPTED_NO.getRepState().equals(upkeepMaintenance.getMtcState())) {
            return DataResult.build9250("维修完成的工单才能指派验收人!");
        }
        UpkeepMaintenance updateUpkeepMaintenance = new UpkeepMaintenance();
        updateUpkeepMaintenance.setId(upkeepMaintenance.getId());
        updateUpkeepMaintenance.setMtcState(EnumRepState.ACCEPTED_YES.getRepState());
        updateUpkeepMaintenance.setAcceptEmployeeId(Long.parseLong(employeeId));
        updateUpkeepMaintenance.setDisAcceptEmployeeId(loginInfo.getLoginEmployeeId());
        upkeepMaintenanceMapper.updateByPrimaryKeySelective(updateUpkeepMaintenance);
        UpkeepWorkOrder updateUpkeepWorkOrder = new UpkeepWorkOrder();
        updateUpkeepWorkOrder.setUpkeepId(upkeepMaintenance.getUpkeepId());
        updateUpkeepWorkOrder.setAcceptEmployeeId(Long.parseLong(employeeId));
        updateUpkeepWorkOrder.setUpkeepState(EnumRepState.ACCEPTED_YES.getRepState());
        upkeepWorkOrderMapper.updateByPrimaryKeySelective(updateUpkeepWorkOrder);
        //记录流水
        insertDisFlow(upkeepMaintenance.getUpkeepId(), updateUpkeepMaintenance.getAcceptEmployeeId(), 3, upkeepMaintenance.getId(), EnumRepState.ACCEPTED_YES.getRepState(), "指定验收人员", loginInfo.getLoginEmployeeId());

        UpkeepWorkOrder getUpkeepWorkOrder = upkeepWorkOrderMapper.selectByPrimaryKey(upkeepMaintenance.getUpkeepId());
        //待办
        commonService.insertMessageBacklog(
                updateUpkeepMaintenance.getAcceptEmployeeId(),
                "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】待现场验收，请尽快处理",
                "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】分配您进行现场验收，请尽快到场验收。维修设备：【" + getUpkeepWorkOrder.getEmCode() + "】-【" + getUpkeepWorkOrder.getEmName() + "】，地点在【" + getUpkeepWorkOrder.getEmLocation() + "】。",
                3, 5, getUpkeepWorkOrder.getUpkeepId(), getUpkeepWorkOrder.getWorkOrderCode()
        );
        return DataResult.build9100();
    }


    /*
     * 验收
     * */
    @Override
    public DataResult acceptUpkeep(LoginInfo loginInfo, String id, String type, String acceptExplain, String acceptScore) throws Exception {
        UpkeepMaintenance upkeepMaintenance = upkeepMaintenanceMapper.selectByPrimaryKey(Long.parseLong(id));
        if (upkeepMaintenance == null) {
            return DataResult.build9250("获取维修人员信息失败！");
        }

//        if (!EnumRepState.ACCEPTED_YES.getRepState().equals(upkeepMaintenance.getMtcState())) {
//            return DataResult.build9250("待验收的工单才能提交验收!");
//        }

        if (!loginInfo.getLoginEmployeeId().equals(upkeepMaintenance.getAcceptEmployeeId())) {
            String errName = commonService.getEmployeeNameById(upkeepMaintenance.getAcceptEmployeeId());
            return DataResult.build9250("人员" + errName + "才能验收操作");
        }
        UpkeepWorkOrder getUpkeepWorkOrder = upkeepWorkOrderMapper.selectByPrimaryKey(upkeepMaintenance.getUpkeepId());
        //维修
        UpkeepMaintenance updateUpkeepMaintenance = new UpkeepMaintenance();
        updateUpkeepMaintenance.setId(upkeepMaintenance.getId());
        //工单
        UpkeepWorkOrder updateUpkeepWorkOrder = new UpkeepWorkOrder();
        updateUpkeepWorkOrder.setUpkeepId(upkeepMaintenance.getUpkeepId());
        //验收
        BusinessAccept businessAccept = new BusinessAccept();
        businessAccept.setSourceId(upkeepMaintenance.getId());
        businessAccept.setBusType(EnumAcceptBusinessType.UPKEEPMAINTENANCE.getBusType());
        businessAccept.setEmployeeId(loginInfo.getLoginEmployeeId());
        businessAccept.setBusEmployeeId(upkeepMaintenance.getEmployeeId());
        if (StrUtil.isNotBlank(acceptExplain)) {
            businessAccept.setAcceptExplain(acceptExplain);
        } else {
            businessAccept.setAcceptExplain("");
        }

        businessAccept.setAcceptScore(new BigDecimal(Optional.ofNullable(acceptScore).orElse("0")));
        businessAccept.setAcceptTime(new Date());
        businessAccept.setCreateTime(new Date());
        int flowState = 0;
        String flowDesc = "";
        //type = 1,验收通过，2，验收驳回
        if ("1".equals(type)) {
            if (!NumberUtil.isNumber(acceptScore)) {
                return DataResult.build9250("评分必须输入数字!");
            }
            businessAccept.setAcceptScore(new BigDecimal(acceptScore));
            flowState = EnumRepState.MAINTENANCE_YES.getRepState();
            flowDesc = "验收通过";
            updateUpkeepWorkOrder.setUpkeepState(EnumRepState.MAINTENANCE_YES.getRepState());
            updateUpkeepMaintenance.setMtcState(EnumRepState.MAINTENANCE_YES.getRepState());
            businessAccept.setAcceptState(1);
            //保养时长
            Long mtcSecond = upkeepMaintenance.getMtcSecond();
            int estimatedWorkHour = getUpkeepWorkOrder.getEstimatedWorkHour();
            updateUpkeepWorkOrder.setWorkHourSecond(mtcSecond);
            //转换成秒
            long estimatedWorkHourSec = estimatedWorkHour * 60;
            //1未超时；2超时
            if (mtcSecond > estimatedWorkHourSec) {
                updateUpkeepWorkOrder.setIsTimeout(2);
            } else {
                updateUpkeepWorkOrder.setIsTimeout(1);
            }
        } else if ("2".equals(type)) {
            businessAccept.setAcceptScore(new BigDecimal(Optional.ofNullable(acceptScore).orElse("0")));
            flowState = EnumRepState.MAINTENANCE_NO.getRepState();
            flowDesc = "验收驳回";
            updateUpkeepWorkOrder.setUpkeepState(EnumRepState.UNDE_REPAIR.getRepState());
            updateUpkeepWorkOrder.setMaintenEmployeeId(upkeepMaintenance.getEmployeeId());
            updateUpkeepMaintenance.setMtcState(EnumRepState.INVALID.getRepState());
            businessAccept.setAcceptState(2);

            //验收驳回新增数据
            UpkeepMaintenance newUpkeepMaintenance = new UpkeepMaintenance();
//            newUpkeepMaintenance.setEmployeeId(upkeepMaintenance.getEmployeeId());
//            newUpkeepMaintenance.setUpkeepId(upkeepMaintenance.getUpkeepId());
//            newUpkeepMaintenance.setDisTime(upkeepMaintenance.getDisTime());
//            newUpkeepMaintenance.setDisEmployeeId(upkeepMaintenance.getDisEmployeeId());
//            newUpkeepMaintenance.setDisType(upkeepMaintenance.getDisType());
            newUpkeepMaintenance.setUpkeepId(upkeepMaintenance.getUpkeepId());
            newUpkeepMaintenance.setMtcState(EnumRepState.UNDE_REPAIR.getRepState());
            newUpkeepMaintenance.setMtcStartTime(upkeepMaintenance.getMtcStartTime());
            if (StrUtil.isBlank(upkeepMaintenance.getAssMtcId())) {
                newUpkeepMaintenance.setAssMtcId(upkeepMaintenance.getId() + "");
            } else {
                newUpkeepMaintenance.setAssMtcId(upkeepMaintenance.getId() + "," + upkeepMaintenance.getAssMtcId());
            }
            newUpkeepMaintenance.setProcessType(5);
            newUpkeepMaintenance.setIsReuse(2);
            upkeepMaintenanceMapper.updateByPrimaryKey(newUpkeepMaintenance);
        }
        businessAcceptMapper.insertSelective(businessAccept);
        upkeepMaintenanceMapper.updateByPrimaryKeySelective(updateUpkeepMaintenance);
        upkeepWorkOrderMapper.updateByPrimaryKeySelective(updateUpkeepWorkOrder);

        //记录流水
        insertFlow(upkeepMaintenance.getUpkeepId(), loginInfo.getLoginEmployeeId(), 4, businessAccept.getId(), flowState, flowDesc);

        //type = 1,验收通过，2，验收驳回
        if ("1".equals(type)) {
            //通知 维修人员
            commonService.insertMessageInform(
                    upkeepMaintenance.getEmployeeId(),
                    "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】已验收通过！",
                    "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】已验收通过，验收打分【" + acceptScore + "】分。【" + acceptExplain + "】",
                    3, 6, getUpkeepWorkOrder.getUpkeepId(), getUpkeepWorkOrder.getWorkOrderCode()
            );
        } else if ("2".equals(type)) {
            //待办
            commonService.insertMessageBacklog(
                    upkeepMaintenance.getEmployeeId(),
                    "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】已驳回，请尽快处理。",
                    "【" + getUpkeepWorkOrder.getWorkOrderCode() + "】验收未通过已驳回，【" + acceptExplain + "】。请尽快处理。",
                    3, 6, getUpkeepWorkOrder.getUpkeepId(), getUpkeepWorkOrder.getWorkOrderCode()
            );
        }

        return DataResult.build9100();
    }

    @Override
    public DataResult updateTask(LoginInfo loginInfo, long id) throws Exception {
        UpkeepTask upkeepTask = upkeepTaskMapper.selectByPrimaryKey(id);
        upkeepTask.setIsComplete(upkeepTask.getIsComplete() == 1 ? 2 : 1);
        upkeepTaskMapper.updateByPrimaryKey(upkeepTask);
        return DataResult.build9100();
    }

    private void insertFlow(Long upkeepId, Long employeeId, int flowType, Long sourceId, Integer state, String flowDesc) {
        UpkeepWorkOrderFlow workOrderFlow = new UpkeepWorkOrderFlow();
        workOrderFlow.setUpkeepId(upkeepId);
        workOrderFlow.setEmployeeId(employeeId);
        workOrderFlow.setSourceId(sourceId);
        workOrderFlow.setDisEmployeeId(0L);
        workOrderFlow.setFlowDesc(flowDesc);
        workOrderFlow.setFlowType(flowType);
        workOrderFlow.setState(state);
        workOrderFlow.setCreateTime(new Date());
        upkeepWorkOrderFlowMapper.insert(workOrderFlow);
    }

    private void insertDisFlow(Long upkeepId, Long employeeId, int flowType, Long sourceId, Integer state, String flowDesc, Long disEmployeeId) {
        UpkeepWorkOrderFlow workOrderFlow = new UpkeepWorkOrderFlow();
        workOrderFlow.setUpkeepId(upkeepId);
        workOrderFlow.setEmployeeId(employeeId);
        workOrderFlow.setSourceId(sourceId);
        workOrderFlow.setDisEmployeeId(disEmployeeId);
        workOrderFlow.setFlowDesc(flowDesc);
        workOrderFlow.setFlowType(flowType);
        workOrderFlow.setState(state);
        workOrderFlow.setCreateTime(new Date());
        upkeepWorkOrderFlowMapper.insert(workOrderFlow);
    }

    private void insertMaintenanceFile(Long busTablesId, Long employeeId, UpkeepMaintenanceInsDto repairMaintenanceDto) {
        //旧的上传图片
        /*if(StrUtil.isNotBlank(repairMaintenanceDto.getFileOldPicCode())){
            if (repairMaintenanceDto.getFileOldPicCode().contains(",")) {
                String[] split = repairMaintenanceDto.getFileOldPicCode().split(",");
                for (String spCode : split) {
                    insertWorkOrderFile(busTablesId, employeeId, spCode, EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 2, 1);
                }
            } else {
                insertWorkOrderFile(busTablesId, employeeId, repairMaintenanceDto.getFileOldPicCode(), EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 2, 1);

            }
        }
        //旧的上传音频
        if(StrUtil.isNotBlank(repairMaintenanceDto.getFileOldAudioCode())){
            if (repairMaintenanceDto.getFileOldAudioCode().contains(",")) {
                String[] split = repairMaintenanceDto.getFileOldAudioCode().split(",");
                for (String spCode : split) {
                    insertWorkOrderFile(busTablesId, employeeId, spCode, EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 2, 2);
                }
            } else {
                insertWorkOrderFile(busTablesId, employeeId, repairMaintenanceDto.getFileOldAudioCode(), EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 2, 2);
            }
        }
        //旧的上传视频
        if(StrUtil.isNotBlank(repairMaintenanceDto.getFileOldVideoCode())){
            if (repairMaintenanceDto.getFileOldVideoCode().contains(",")) {
                String[] split = repairMaintenanceDto.getFileOldVideoCode().split(",");
                for (String spCode : split) {
                    insertWorkOrderFile(busTablesId, employeeId, spCode, EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 2, 3);
                }
            } else {
                insertWorkOrderFile(busTablesId, employeeId, repairMaintenanceDto.getFileOldVideoCode(), EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 2, 3);
            }
        }*/
        //新的上传图片
        if (StrUtil.isNotBlank(repairMaintenanceDto.getFileNewPicCode())) {
            if (repairMaintenanceDto.getFileNewPicCode().contains(",")) {
                String[] split = repairMaintenanceDto.getFileNewPicCode().split(",");
                for (String spCode : split) {
                    insertWorkOrderFile(busTablesId, employeeId, spCode, EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 3, 1);
                }
            } else {
                insertWorkOrderFile(busTablesId, employeeId, repairMaintenanceDto.getFileNewPicCode(), EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 3, 1);
            }
        }
        //新的上传音频
        if (StrUtil.isNotBlank(repairMaintenanceDto.getFileNewAudioCode())) {
            if (repairMaintenanceDto.getFileNewAudioCode().contains(",")) {
                String[] split = repairMaintenanceDto.getFileNewAudioCode().split(",");
                for (String spCode : split) {
                    insertWorkOrderFile(busTablesId, employeeId, spCode, EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 3, 2);
                }
            } else {
                insertWorkOrderFile(busTablesId, employeeId, repairMaintenanceDto.getFileNewAudioCode(), EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 3, 2);
            }
        }
        //新的上传视频
        if (StrUtil.isNotBlank(repairMaintenanceDto.getFileNewVideoCode())) {
            if (repairMaintenanceDto.getFileNewVideoCode().contains(",")) {
                String[] split = repairMaintenanceDto.getFileNewVideoCode().split(",");
                for (String spCode : split) {
                    insertWorkOrderFile(busTablesId, employeeId, spCode, EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 3, 3);
                }
            } else {
                insertWorkOrderFile(busTablesId, employeeId, repairMaintenanceDto.getFileNewVideoCode(), EnumBusinessType.UPKEEPMAINTENANCE.getBusType(), 3, 3);
            }
        }
    }

    private void insertWorkOrderFile(Long busTablesId, Long employeeId, String fileCode, int busType, int fileGroup, int fileType) {
        BusinessFile businessFile = new BusinessFile();
        businessFile.setBusTablesId(busTablesId);
        businessFile.setBusType(busType);
        businessFile.setFileGroup(fileGroup);
        businessFile.setEmployeeId(employeeId);
        businessFile.setFileCode(fileCode);
        businessFile.setFileType(fileType);
        businessFile.setCreateTime(new Date());
        businessFileMapper.insertSelective(businessFile);
    }

}
